### MAP of CSO Restrictions ####
### 6.5.2016 ###################


rm(list=ls())

### load packages
library(foreign)
library(readstata13)
library(rworldmap)
library(countrycode)

setwd("C:/Users/smidt/Dropbox/PC/Documents/PaperProjects/Paper-Cracking Down on HR defenders/Analysis/Code/Replication")
data <- read.dta13("RestrictionsFinalData.dta")
attach(data)


## Define a variable ISO3V10 that indicates the location of the 
## country on the map
data$codes.of.origin <- data$cowcode # Vector of values to be converted
data$ISO3V10<-countrycode(data$codes.of.origin, "cown", "iso3c")
length(data$ISO3V10)
length(data$codes.of.origin)

## Data on CSO restrictions
table(data$activitiesRestrict)
data$activitiesRestrict[data$activitiesRestrict==0]=2


##################
## Make maps #####
##################

## Maps for 2014
data2014<-subset(data,YEAR==2014)
data2014<-subset(data2014,!is.na(restrictionCount2))

# Join data to map for 2011
sPDF2014<-joinCountryData2Map(data2014
                              ,joinCode="ISO3"
                              ,nameJoinColumn = "ISO3V10")

# Map continous
png('./Figures/Figure1.png', width = 4, height = 4, units = 'in', res = 400)
op=par(mfcol=c(1,1), mai=c(.2,.2,.2,.2),xaxs="i",yaxs="i")
map<-mapCountryData(sPDF2014
                    ,nameColumnToPlot="restrictionCount2"
                    ,mapTitle=""
                    #,mapTitle="Government restrictions against CSOs"
                    ,colourPalette="white2Black"
                    ,catMethod='categorical'
                    ,borderCol='black'
                    ,addLegend=F)
#title(main=paste("Government restrictions in 2014"), cex=0.3, line=0)
map$legendText <- c('0', '1','2', '3', '4', '5', '6', '7', '8')
do.call(addMapLegendBoxes, c(map,x=-175,y=-90,horiz=T,title="Number of government restrictions in 2014",cex=.6, pt.cex=2,col="gray", bty="n"))
dev.off()


